﻿
TYPE_Normal     = 'Normal' 
TYPE_Payload    = 'Payload'
TYPE_Error      = 'Error'
TYPE_Asyn       = 'Asynchronous'

TYPE_Request    = 'request'
TYPE_Response   = 'response'

New_Line        = '\r\n'
Trans_ID        = 0
DEBUG = True

def logInfo(msg):
    msg = 'INFO: ' + msg
    print(msg)

def logDebug(msg):
    if (DEBUG):
        msg = 'DEBUG: ' + msg
        print(msg)
    
def genCommansString(msnPkg):
    if msnPkg.type == TYPE_Normal:
        return genNormalCommand(msnPkg)
    
    if msnPkg.type == TYPE_Payload:
        pass
        
    if msnPkg.type == TYPE_Error:
        pass
        
    if msnPkg.type == TYPE_Asyn:
        pass
    
def genNormalCommand(msnPkg):
    command = msnPkg.cmd
    command = command + ' ' + msnPkg.transID
    
    for para in msnPkg.parameters:
        command = command + ' ' + para
    
    command = command + ' ' + New_Line
    
    #Convert to bytes type, not used here
    #command = command.encode()
    
    logDebug(command)
    return command
    
#############################################
#
# Receive a string from server
# Analyze it, and parse it to be correct command
#
def parseCommand(msnPkg, string):
    pass
    
#################################
# increase the trans id after 
# create each normal packages
#
def increaseTransID():
    global Trans_ID
    Trans_ID += 1
    
    
class MsnPkg:
    ''' MSN packages during the negotiation of client and server '''
    
    ###########################################
    # direction:    request     client->server
    #               response    server->client
    #
    def __init__(self, direction):
        self.type_ = ''
        self.cmd  = ''
        self.transID = -1
        #self.parameters = []
        self.direction = direction
        
        if direction == TYPE_Request:
            increaseTransID()

###############################################
# Authentication commands:
# VER - Protocol version
# CVR - Sends version information
# INF - List supported authentication systems, now obsolete
# USR - Authentication command
# XFR - Redirection to Notification Server
#
class MsnPkgVer(MsnPkg):
    
    def __init__(self, direction):
        MsnPkg.__init__(self, direction)
        self.type = TYPE_Normal
        self.cmd  = 'VER'
        if direction == TYPE_Request:
            self.transID = str(Trans_ID)
    
    def addData(self, protocals):
        self.protocals = protocals
        self.parameters = ['CVR0']
        
        #self.parameters.append(protocal)
        #self.parameters.append('CVR0')
    
    #####################################
    # Generate command used to send out
    def getCommandString(self):
        command = self.cmd
        command = command + ' ' + self.transID
        command = command + ' ' + self.protocals
        
        for para in self.parameters:
            command = command + ' ' + para
        
        command = command + ' ' + New_Line
        
        logDebug(command)
        return command
    
    ##############################################
    # Recv the command string received from server 
    #  and parse them
    #
    #  E.g: VER 1 MSNP8 MSNP9 MSNP10 CVR0\r\n
    #
    def setParameters(self, string):
        string_new = string.replace('\r\n','')
        para_list = string_new.split(' ')
        
        self.cmd = para_list[0]
        self.transID = para_list[1]
        self.protocals = ''
        self.parameters = []
        
        for index in range(2, len(para_list)):
            if para_list[index] != 'CVR0':
                self.protocals = self.protocals + ' ' + para_list[index]
    
class MsnPkgCvr(MsnPkg):

    def __init__(self, direction):
        MsnPkg.__init__(self, direction)
        self.type = TYPE_Normal
        self.cmd  = 'CVR'
        self.transID = str(Trans_ID)
        self.constString = '0x0409 win 4.10 i386 MSNMSGR 5.0.0544 MSMSGS'
        
    def addData(self, passport):
        self.passport = passport
    
    #####################################
    # Generate command used to send out
    def getCommandString(self):
        command = self.cmd
        command = command + ' ' + self.transID
        command = command + ' ' + self.constString
        command = command + ' ' + self.passport
        command = command + ' ' + New_Line
        
        logDebug(command)
        return command

    ##############################################
    # Recv the command string received from server 
    #  and parse them
    #
    #  E.g: VER 1 MSNP8 MSNP9 MSNP10 CVR0\r\n
    #
    def setParameters(self, string):
    
        para_list = string_new.split(' ')
        self.cmd = para_list[0]
        self.transID = para_list[1]
        
        self.recommmandVersion = para_list[2]
        self.identical = para_list[3]
        self.minimumVersion = para_list[4]
        self.downloadURL = para_list[5]
        self.moreInfoURL = para_list[6]


if __name__ == '__main__':
    msnPkg = MsnPkgVer(TYPE_Request)
    msnPkg.addData('MSNP8')
    msnPkg.getCommandString()
    
    msnPkg_new = MsnPkgVer(TYPE_Response)
    msnPkg_new.setParameters('VER 1 MSNP8 MSNP9 MSNP10 CVR0\r\n')
    msnPkg_new.getCommandString()
    
    
    #increaseTransID()
    
    msnPkg2 = MsnPkgCvr(TYPE_Request)
    msnPkg2.addData('maocat525@163.com')
    msnPkg2.getCommandString()
    
    
    